home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 27.zip / BS1 part 27 / ImageMaster_d3.adf / piarc.lzh.parta / jpeg.rexx < prev    next >
OS/2 REXX Batch file  |  1993-04-14  |  10KB  |  386 lines

  1. /*
  2.  * JPEG.rexx
  3.  *
  4.  *  Written by: Ben Williams
  5.  * Last Update: April 6th, 1993
  6.  *         For: Black Belt Systems image processing series IM, IM F/c, and IP.
  7.  * ---------------------------------------------------------------------------
  8.  *    Revision: 1.12
  9.  */
  10. parse arg var1 ' ' fullname
  11.  
  12. /*
  13.  * open rexxsupport.library -- needed for some functions
  14.  */
  15. if ~show('L',"rexxsupport.library") then do
  16.   if addlib('rexxsupport.library',0,-30,0) then do
  17.       /* everything's ok */
  18.     end;
  19.   else do
  20.     say 'We Have A Library Problem, Unable To Load "rexxsupport.library"';
  21.     say 'Cannot operate JPEG.rexx without this library - sorry!';
  22.     'finish';
  23.     exit 10;
  24.     end;
  25.   end;
  26.  
  27. /*
  28.  * This will automatically direct the script to the proper
  29.  * software, if it is running.
  30.  */
  31. prtnme = 'IP_Port'; /* assume Image Professional */
  32. if show('P','IP_Port') = 0 then do
  33.   if show('P','IM_Port') = 0 then do
  34.     say "Can't find image processor's ARexx port!!!"; /* not running? */
  35.     say "This script requires IP, IM or IM F/c to run!";
  36.     exit(20);
  37.     end;
  38.   else do
  39.     prtnme = 'IM_Port'; /* That's the thing about assumptions... */
  40.     end;                 /* We make em, user's break em.          */
  41.   end;
  42.  
  43.   /*
  44.    * This code attempts to read a file called "picmdpath" from REXX:
  45.    * If it can't find it, the script will assume that the commands
  46.    * associated with this PI Module are in "c:". If the file exists,
  47.    * the script will look in the path that is specified in the file.
  48.    * If you create this file, you MUST put a complete, correct path
  49.    * in it; if the commands are in a sub-directory, you have to put
  50.    * the trailing slash on the path (like, device:dir/).
  51.    * 
  52.    */
  53.   cmdpath = 'c:';
  54.   if open(fhandle,'rexx:picmdpath','read') then  /* open the file */
  55.     do
  56.       cmdpath = readln(fhandle);
  57.       call close(fhandle);  /* close the file    */
  58.     end
  59.  
  60. /*
  61.  * Prompt user - load, or save?
  62.  */
  63. if var1 = 'load' then do
  64.   pick = 1
  65.   end
  66. else if var1 = 'save' then do
  67.   pick = 2
  68.   end
  69. else do
  70.     address(prtnme);
  71.     options results;
  72.     'gadgets "Load","JFIF/JPEG","Save","JFIF/JPEG"';
  73.     pick = result;
  74.     options;
  75.     address;
  76.     end;
  77.  
  78. if pick=0 then do
  79.   exit 0;
  80.   end;
  81.  
  82. /*
  83.  * PI driver for compressor
  84.  */
  85. if pick=2 then do /* compression */
  86.  
  87.   bufname = 'image';
  88.   strn = ' -q ';
  89.   address(prtnme);
  90.  
  91.   options results;
  92.   'askprop "Compression/Damage Factor","0","0","100"';
  93.   qfact = result;
  94.   'askyn "Good compression","Best Compression"';
  95.   compr = result;
  96.   options;
  97.  
  98.   if compr = 1 then do
  99.     strn = ' -E'||strn;
  100.     end;
  101.  
  102.   prevpath = 'ram:'; /* put user in ram to start with... */
  103.   if show('C',jpegpath) = 1 then do
  104.     prevpath = getclip(jpegpath);
  105.     end;
  106.  
  107.   options results;
  108.   'current';
  109.   bufdata = result; /* get name of buffer, if there is one */
  110.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  111.   if bname ~= '<none>' then do
  112.     bufname = bname;
  113.     end;
  114.   if (length(bufname) > 4) then do
  115.     epos = pos('.jpg',bufname,length(bufname)-4);
  116.     if epos ~= 0 then do
  117.       bufname = left(bufname,epos-1)
  118.       end
  119.     end;
  120.   'filerequest "'||prevpath||'","'||bufname||'",".jpg","Save JPEG"';
  121.   jpegfile = result;
  122.   
  123.   'getdpi '||bnum;
  124.   parse var result fxdpi ',' fydpi; /* get image DPI to write to file */
  125.   options;
  126.   
  127.   /*
  128.    * This strips the floating point info to integer portions only:
  129.    */
  130.   parse var fxdpi xdpi '.' fractional
  131.   parse var fydpi ydpi '.' fractional
  132.   xdpi = strip(xdpi,'B',' ');
  133.   ydpi = strip(ydpi,'B',' ');
  134.   tline = xdpi||','||ydpi;
  135.   /*
  136.    * create temp file:
  137.    */
  138.   call open(thandle,'ram:jp_dpi_temp','write');
  139.     writeln(thandle,tline);
  140.     call close(thandle);
  141.  
  142.   if jpegfile = 'FR_CANCELLED' then do
  143.     address(prtnme);
  144.     exit 0;
  145.     end;
  146.  
  147. jpegfile = expandfilename(jpegfile); /* make filename complete path */
  148. thispath = gimmepath(jpegfile);
  149. call setclip(jpegpath,thispath);
  150.  
  151.   address(prtnme);
  152.   options results;
  153.   'jackin';
  154.   jackadr = result;
  155.   options;
  156.  
  157.   address command cmdpath||'wrjpg >"'||jpegfile||'" -j'||jackadr||strn||qfact;
  158.   address command "delete ram:jp_dpi_temp QUIET";
  159.  
  160.   address(prtnme);
  161.   address;
  162.  
  163.   exit 0;
  164.  
  165.   end;
  166.  
  167. else do /* pick=1 decompression */
  168.  
  169. if fullname = "" then do
  170.     /*
  171.      * Setup default path
  172.      */
  173.       prevpath = 'ram:'; /* put user in ram to start with... */
  174.     /*
  175.      * Now, get old path if it exists
  176.      */
  177.       if show('C',jpegpath) = 1 then do
  178.         prevpath = getclip(jpegpath);
  179.         end;
  180.     
  181.       address(prtnme);
  182.       options results;
  183.       'current';
  184.       bufdata = result; /* get name of buffer, if there is one */
  185.       parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  186.       if bname ~= '<none>' then do
  187.         bufname = bname;
  188.         end;
  189.       'filerequest "'||prevpath||'","'||bufname||'","","Load JPEG"';
  190.       jpegfile = result;
  191.       options;
  192.     
  193.       if jpegfile = 'FR_CANCELLED' then do
  194.         address(prtnme);
  195.         'finish';
  196.         exit 0;
  197.         end;
  198.     
  199.       jpegfile = expandfilename(jpegfile);
  200.     end;
  201. else do /* fullname defined on entry to the macro */
  202.   jpegfile = expandfilename(fullname);
  203. end;
  204.   thispath = gimmepath(jpegfile);
  205.   call setclip(jpegpath,thispath);
  206.  
  207.   fileinfo = statef(jpegfile);
  208.   parse var fileinfo fitype fibytes fiblocks fiflags fidays fimins fiticks ficomment;
  209.  
  210.   if fitype = '' then do
  211.     'message Cannot locate "'jpegfile'" for processing';
  212.     'finish';
  213.     exit 10;
  214.     end;
  215.  
  216.   if fibytes < 170 then do
  217.     "message This file is too small to be a JFIF formatted JPEG file";
  218.     'finish';
  219.     exit 10;
  220.     end;
  221.  
  222.   if fitype = 'DIR' then do
  223.     'message Must specify a file, not a directory';
  224.     'finish';
  225.     exit 10;
  226.     end;
  227.  
  228. /*
  229.  * at this point, we have at least some assurance that we
  230.  * have a real JPEG file to work with. Now, we need to look into
  231.  * the file and see how big the image is, so we can open a new
  232.  * buffer of the appropriate size.
  233.  */
  234.  
  235.   call open(fhandle,jpegfile,'read');      /* open the file     */
  236.   if rvalue() ~= 65496 then do /* FFD8 */
  237.     'message Not a JFIF file! - Initial read fails';
  238.     call close(fhandle);
  239.     'finish';
  240.     exit(0);
  241.     end;
  242.  
  243.   work=1;
  244.   do while work = 1
  245.     thisid = rvalue();
  246.     thisln = rvalue();
  247.     thisda = readch(fhandle,thisln-2);
  248.     select
  249.       when thisid = 65472 | thisid < 65488 then do /* FFC0-FFCF (SOF0-15) */
  250.         height = c2d(substr(thisda,2,2));
  251.         width  = c2d(substr(thisda,4,2));
  252.         work=0;
  253.         end;
  254.       when thisid = 65504 then do  /* FFE0 */
  255.         fid = left(thisda,4);
  256.         if fid ~= 'JFIF' then do
  257.           'message Not a JFIF file!';
  258.           call close(fhandle);
  259.           'finish';
  260.           exit 0;
  261.           end;
  262.         end;
  263.       otherwise do
  264.         nop;
  265.         end;
  266.       end;
  267.     end;
  268.  
  269.   call close(fhandle);                     /* close the file    */
  270.  
  271.   if height < 0 then do
  272.     "message Bad height: "||height;
  273.     'finish';
  274.     exit 0;
  275.     end;
  276.  
  277.   if height > 32767 then do
  278.     "message Bad height: "||height;
  279.     'finish';
  280.     exit 0;
  281.     end;
  282.  
  283.   if width < 0 then do
  284.     "message Bad width: "||width;
  285.     'finish';
  286.     exit 0;
  287.     end;
  288.  
  289.   if width > 32767 then do
  290.     "message Bad width: "||width;
  291.    'finish';
  292.     exit 0;
  293.     end;
  294.  
  295.   address(prtnme);
  296.   
  297.   /* New buffer is created at current resolution */
  298.   address(prtnme);
  299.   'autoredraw 0';
  300.   
  301.   options results;
  302.   
  303.   'newtargetted '||width||' '||height||' "'||gxname||'"'
  304.   if rc ~= 0 then do
  305.     options;
  306.     "message Can't allocate buffer, error #"||rc;
  307.     'autoredraw 1';
  308.     'finish';
  309.     exit 0;
  310.     end
  311.   bnum = result;
  312.   
  313.   'backin '||bnum;
  314.   jackadr = result;
  315.   options;
  316.  
  317.   'lockimage '||bnum;
  318.   address command cmdpath||'rdjpg -j'||jackadr||' "'||jpegfile||'"';
  319.   call open(thandle,'ram:jp_dpi_temp','read');      /* open the file     */
  320.     dpidpi = readln(thandle);
  321.     call close(thandle);
  322.   parse var dpidpi xdpi ',' ydpi;
  323.   'setdpi '||bnum||' '||xdpi||' '||ydpi;
  324.   address command "delete ram:jp_dpi_temp QUIET";
  325.   'unlockimage '||bnum;
  326.  
  327.   address(prtnme);
  328.   'autoredraw 1';
  329.   'finish';
  330.   address;
  331.  
  332.   exit 0;
  333.  
  334. end;
  335.  
  336. /*
  337.  * gimmepath
  338.  *
  339.  * This takes the provided argument and sucks the path out of it, then
  340.  * returns that path to the caller, sans file name.
  341.  */
  342. gimmepath:
  343.   arg fullnamegx;
  344.     tempgx = reverse(fullnamegx);
  345.     lengx = length(fullnamegx);   /* get length of string */
  346.     slashdex = index(tempgx,'/'); /* first occurance of '/' from right */
  347.     colondex = index(tempgx,':');  /* first occurance of ':' from right */
  348.     seploc = 0; /* assumes current dir, no path supplied */
  349.     if slashdex ~= 0 then do /* we assume we are in a DIR */
  350.       seploc = (lengx - slashdex)+1;
  351.       end;
  352.     else do
  353.       if colondex ~= 0 then do /* we assume we are on a device */
  354.         seploc = (lengx - colondex)+1;
  355.         end;
  356.       end;
  357.   gxname = substr(fullnamegx,seploc+1); /* if you ever need it */
  358.   gxpath = left(fullnamegx,seploc);
  359.   return(gxpath);
  360.  
  361. /*
  362.  * Since this script can't be expected to know where the CD of the user
  363.  * is when this cmd is invoked, we have to check the path the user
  364.  * provides - if it's not specified right from a root, then we have
  365.  * to make it a complete specification from the root.
  366.  */
  367. expandfilename:
  368.   parse arg jfile;
  369.   if index(jfile,':') = 0 then do
  370.     curdir = pragma(D);
  371.     if right(curdir,1) ~= ':' then do
  372.       if right(curdir,1) ~= '/' then do
  373.         if curdir ~= '' then do
  374.           curdir = curdir || '/';
  375.           end;
  376.         end;
  377.       end;
  378.     jfile = curdir||jfile;
  379.     end;
  380.   return(jfile);
  381.  
  382. rvalue:
  383.   wordnum = c2d(readch(fhandle,1)) * 256;
  384.   wordnum = wordnum + c2d(readch(fhandle,1));
  385.   return wordnum;
  386.